package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.utils.MD5Util;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.mapper.LoginUserMapper;
import com.dongjiantong.user.model.LoginUser;
import com.dongjiantong.user.enums.UserEnum;
import com.dongjiantong.user.service.SSOLoginService;
import lombok.extern.slf4j.Slf4j;


import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cao on 2017/11/7.
 */
@Slf4j
@Service(version = "1.0.0")
public class SSOLoginServiceImpl extends ServiceMybatis<LoginUser> implements SSOLoginService {


    @Resource
    private LoginUserMapper loginUserMapper;



    @Override
   public Response<LoginUser> doLogin(String username, String password)
   {

//       Response<LoginUser> result = checkLogin(username,password);
//
//       //登录不成功
//       if(!result.isSuccess())
//       {
//           return Response.fail(HttpCode.LOGIN_FAIL,result.getMessage());
//       }
//
//        return result;

        return checkLogin(username,password);
   }


   @Override
   public Response<Boolean> checkUserName(String username)
   {
       boolean isExist = false;
       LoginUser loginUser =  loginUserMapper.checkUserName(username);
       if(loginUser != null) {
           isExist = true;
       }
       return Response.ok(isExist);
   }


    @Override
    public boolean updateUserLoginStatus(long userId,UserEnum userEnum)
    {
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("status",userEnum.getCode());
        map.put("userId",userId);
        int result =  loginUserMapper.updateUserLoginStatus(map);

        return  result>0;
    }

    /**
     * 登录验证
     * @param username
     * @param password
     * @return
     */

    private Response<LoginUser> checkLogin(String username, String password)
    {

        LoginUser loginUser =  loginUserMapper.checkUserName(username);

        if(loginUser == null)
        {
            log.info("user(UserName={}) not found", username);

            return Response.fail(HttpCode.NOT_FOUND,"登录帐号不存在");

        }

        //帐号禁用
        if(loginUser.getStatus()==1)
        {
            return Response.fail(HttpCode.LOCKED,"登录帐号已被禁用");
        }

        //密码不对
        if(!checkPassword(password,loginUser.getPassword(),loginUser.getSalt()))
        {
            loginUser.setErrorcount(loginUser.getErrorcount()+1);
            loginUser.setUnlocktime(new Date());
            loginUserMapper.updateByPrimaryKey(loginUser);


            return Response.fail(HttpCode.LOGIN_FAIL,"登录帐号密码不正确");
        }


        loginUser.setLogincount(loginUser.getLogincount()+1);
        loginUserMapper.updateByPrimaryKey(loginUser);

        return Response.ok(loginUser);
    }

    /**
     * 检查密码
     * @param password
     * @param dbpassword
     * @param salt
     * @return
     */
    private boolean checkPassword(String password,String dbpassword,String salt)
    {

        String newPassword= MD5Util.encrypt(salt+password);

        if(newPassword.equals(dbpassword))
        {
            return true;
        }

         return false;

    }



}
